<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/22
 * Time: 11:56
 */

namespace app\admin\service;

use app\models\ContractModel;
use app\models\CourseModel;
use app\models\EdubossModel;
use app\models\PackageModel;
use app\models\StudentModel;

/**
 * Class CustomerService
 * @package app\admin\service
 */
class CustomerService
{
    /**
     * 我的客户
     * @return array
     */
    public function eduboss()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $name = input('name', '');
            $phone = input('phone', '');
            $student_grade = input('student_grade', '');
            $register_time = input('register_time', '');
            $channel = input('channel', '');
            $where = [];
            if ($name) $where['name'] = $name;
            if ($phone) $where['phone'] = $phone;
            if ($student_grade) $where['student_grade'] = $student_grade;
            if ($register_time) $where['register_time'] = $register_time;
            if ($channel) $where['channel'] = $channel;
            $type = input('type', 'all');
            $order = 'id desc';
            switch ($type) {
                case 'today':
                    //今天待跟进
                    $date = date('Y-m-d');
                    $start = $date . ' 00:00:00';
                    $end = $date . ' 23:59:59';
                    $where['register_time'] = [['egt', $start], ['lt', $end]];
                    $customers = EdubossModel::pageLists($where, $page, $listRows, $order);
                    break;
                case 'tomorrow':
                    // 明天待跟进
                    $date = date("Y-m-d", strtotime("+1 day"));
                    $start = $date . ' 00:00:00';
                    $end = $date . ' 23:59:59';
                    $where['register_time'] = [['egt', $start], ['lt', $end]];
                    $customers = EdubossModel::pageLists($where, $page, $listRows, $order);
                    break;
                case 'week':
                    //本周待跟进
                    $time = 'week';
                    $customers = EdubossModel::pageLists($where, $page, $listRows, $order, $time);
                    break;
                case 'key_university':
                    //重点跟进
                    $where['key_universities'] = 1;
                    $customers = EdubossModel::pageLists($where, $page, $listRows, $order);
                    break;
                default:
                    $customers = EdubossModel::pageLists($where, $page, $listRows, $order);
                    break;
            }
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function read_course()
    {
        try {
            $data = input('post.');

        } catch (\Exception $ex) {
            if(($ex->getCode()) == 10501){
                return ['code' => 1, 'msg' => '手机号码已存在'];
            }
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function balance()
    {
        try {
            $data = input('post.');
            $student = StudentModel::where('id',$data['student_id'])->find();
            $data['student_name'] = $student['name'];
            $data['grade'] = $student['grade'];
            $data['class'] = $student['class'];
            $data['total_amount'] = $data['total_amount'] * 100;
            $data['preferential'] = $data['preferential'] * 100;
            $data['stay_amount'] = $data['stay_amount'] * 100;
            unset($data['totalprice']);
            $res = ContractModel::create($data);
            $arr = array(
                'id' => $res->id,
                'contract_number' => date('Ymd').$res->id
            );
            ContractModel::update($arr);
            $contract = ContractModel::get($res->id);
            /*报读课程记录*/
            CommonService::writeCourseRecord(date('Ymd').$res->id,$res->course,$res->campus);
            /*生成已签约客户*/
            CommonService::writeSignContract($student['id'],$contract['contract_number'],$contract['contract_type']);
            return ['code' => 0, 'msg' => "保存订单成功"];
        } catch (\Exception $ex) {
            if(($ex->getCode()) == 10501){
                return ['code' => 1, 'msg' => '合成已存在'];
            }
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * 跟进
     * @return array
     */
    public function follow_up()
    {
        try {
            $data = input('post.');
            $data['birth'] = input('birth') ? input('birth') : NULL;
            EdubossModel::update($data);
            return ['code' => 0, 'msg' => "修改成功"];
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => "修改失败"];
        }
    }
}